package com.fingard.dsp.bank.directbank.hxb01;

import java.text.MessageFormat;
import java.util.Calendar;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;
import com.fingard.util.DateHelper;

/**
 * @author fujia
 * @deprecated:华夏银行查询余额处理类
 * @time 20160825
 */
public class HXB01Balance extends HXB01Base implements IBalance {

    /**
     * 单个余额查询，统一采用流处理方式
     * 1、循环调用Req7506BalQry.readRecord()读取一个请求明细，然后写入发银行的XML对象
     * 2、发银行
     * 3、循环调用XmlTextReader.read()读取一个银行返回节点，赋值返回ATS的明细对象，
     * 然后调用RetAts7506BalQry.writeRetDetail()，写入返回ATS的明细
     * 单账户余额xhj0004
     */
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {//余额查询
        //余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {

            while (p_req7506.readRecord()) {//不支持批量查询，逐个查询
                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                String sendInfo = MessageFormat.format("{0}#{1}#@@@@", "xhj0004", p_req7506.readedDetail.actNum);
                //测试代码，打印报文
//				System.out.println("发生银行报文:"+sendInfo);
                String[] tmpStrRet = sendToBank(sendInfo);
                //如果返回tmpStrRet[0]表示返回connMsg，connMsg 为空则表示响应正常，
                //测试代码------
//				String[] tmpStrRet={"",""};
//				tmpStrRet[0]="";
//				tmpStrRet[1]="000000#10092102129912129#1000.21#8000.00#Note1#Note12#@@@@";
                if (tmpStrRet[0].length() == 0) {
                    String[] resultArr = tmpStrRet[1].split("#");
                    if (resultArr[0].equals("000000")) {
                        tmpRetDetail.respCode = "0";
                        //获取币种
                        tmpRetDetail.actCur = p_req7506.readedDetail.actCur;
                        //获取账户
                        tmpRetDetail.actNum = resultArr[1];
                        //获取实际余额
                        tmpRetDetail.currBal = resultArr[2];
                        //获取可用余额
                        tmpRetDetail.availBal = resultArr[3];
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = convertRespToBank(resultArr[0]);//获取银行返回信息"查询失败，银行返回码："+resultArr[0]
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpStrRet[1];
                }
                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        RetAts7505HisBalQry retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req7505.getDateFrom());

            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req7505.getDateTo());

            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            //按自然日循环查询
            while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
                String sendInfo = MessageFormat.format("{0}#{1}#{2}#@@@@", "xhj0003", p_req7505.actNum, Format.Date8Format.format(tmpCalendarFrom.getTime()));
                String[] tmpStrRet = sendToBank(sendInfo);
                if (tmpStrRet[0].length() == 0) {
                    String[] resultArr = tmpStrRet[1].split("#");
                    if (resultArr[0].equals("000000")) {
                        RetAtsHisBalDetail tmpRetDetail = new RetAtsHisBalDetail();
                        //交易日期
                        String tmpETYDAT = resultArr[2];
                        if (tmpETYDAT.length() == 8) {
                            tmpRetDetail.balDate = tmpETYDAT.substring(0, 4) + "-" + tmpETYDAT.substring(4, 6) + "-" + tmpETYDAT.substring(6, 8);
                        } else {
                            tmpRetDetail.balDate = tmpETYDAT;
                        }
                        //余额
                        tmpRetDetail.actBal = resultArr[3];
                        retAts.writeRetDetail(tmpRetDetail);
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = convertRespToBank(resultArr[0]);//获取银行返回信息"查询失败，银行返回码："+resultArr[0]
                        break;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpStrRet[1];
                    break;
                }
                tmpCalendarFrom.add(Calendar.DATE, 1);
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("返回结果为空，所查询时间段内不存在历史余额");
        }
        retAts.writeEnd();
        return retAts;
    }

}
